//
//  SetUserInfoEngine.m
//  HuLaQuan
//
//  Created by hok on 2/2/16.
//  Copyright © 2016 yuwubao. All rights reserved.
//

#import "SetUserInfoEngine.h"

#import "CurrentUser.h"

#import "UIImage+YuExt.h"
@implementation SetUserInfoEngine


- (void)setUserInfo:(id)info withTag:(NSString *)tag
{
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *infoString;
    if ([info isKindOfClass:[UIImage class]]) {
         infoString =[info compressAndConvertToBase64];
    }else{
        infoString = info;
    }
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"setUserInfo\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\"><%@>%@</%@><attach>attach</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID,tag,infoString ? infoString : @"",tag];
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CUSTOMER parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            if ([item isKindOfClass:[NSArray class]]) {
                NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
                
                NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
                
                
                
                NSLog(@"%@",resultDic);
                
                
                //                NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
                //                [accountInfo setObject:[arr lastObject] forKey:[arr firstObject]];
                
                result = @{@"isSuccess":@YES,@"result":tag};
                
            }
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
            NSLog(@"%@",item);
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"SetUserInfoFinish" object:result];
    }];

}



@end
